/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"0A669A42-2DAC-4E72-BB0C-08853D34D8C5",variableType:4}
 */
var fConciliado = 2;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"93E1247C-AF98-4086-8244-A67E15B1E12B",variableType:4}
 */
var fCuenta = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"E7D3F906-E71C-40A4-AE12-9F506F4B4E6C",variableType:8}
 */
var fDifSaldos = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"DADE69BA-1370-4AD9-93DB-3B54E3D2D009",variableType:8}
 */
var fEgresos = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"859DE755-0C9A-4B57-9658-D3003292DDC3",variableType:93}
 */
var fFechaDesde = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"E75C51E8-4D17-4B27-90B4-1DD5BE617113",variableType:93}
 */
var fFechaHasta = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"0A9F5E26-DBA4-441E-BDD9-6DC154C9278C",variableType:8}
 */
var fIngresos = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"BFC69B2F-1E7B-4EBF-9686-68F6EB723BFC",variableType:4}
 */
var fNumEgr = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"E2089414-514B-4ABE-830C-FBDA87E9E2CB",variableType:4}
 */
var fNumIng = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"C04087FF-47C7-4AF5-BE61-79FB91C20C8A",variableType:4}
 */
var fNumMovim = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"043D6AA6-837E-48D6-8FA3-3AEE0AB0EE2C",variableType:4}
 */
var fSaldoBanco = null;

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"1F574785-FFAB-4902-BBA3-9A371832E2E8"}
 * @AllowToRunInFind
 */
function buscar(event) {
	if (globals.gCuentaConc != null && fFechaDesde != null && fFechaHasta != null)
	{
		if (controller.find())
		{
			if (fConciliado == 1)
				saldos_banco_id = globals.gBancoSaldo
			if (fConciliado != 2)
				marca_conciliacion = fConciliado

			cuentas_cyb_id = globals.gCuentaConc

			var Searchdate = utils.dateFormat(fFechaDesde,'dd-MM-yyyy')+'...'+utils.dateFormat(fFechaHasta,'dd-MM-yyyy')
			fecha_movim = '#'+Searchdate +'|dd-MM-yyyy'

			var f = controller.search()
			if (f == 0)
				globals.DIALOGS.showWarningDialog('Aviso','No se encontraron resultados','OK')
		}
		else
		{
			globals.DIALOGS.showWarningDialog('Aviso','No se puede buscar','OK')
		}
	}
	else
	{
		globals.DIALOGS.showWarningDialog('Aviso','Por favor ponga todas las condiciones','OK')
	}
	fEgresos 	= 0
	fIngresos 	= 0
	fNumEgr 	= 0
	fNumIng 	= 0
	fNumMovim 	= 0
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"76CDB426-AD52-42B6-B47A-49340202950E"}
 */
function imprimir(event) {
	var $imp = forms.concialiacion_impresion
	
	$imp.fCuenta 			= forms.concialiacion_tbl.movim_cyb_to_cuentas_cyb.descripcion
	$imp.fFechaDesde 		= utils.dateFormat(fFechaDesde,'dd-MM-yyyy')
	$imp.fFechaHasta 		= utils.dateFormat(fFechaHasta,'dd-MM-yyyy')
	$imp.fBanco_saldo_id 	= globals.gBancoSaldo
	$imp.fDifSaldos 		= fDifSaldos
	$imp.controller.showPrintPreview()
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"D132E6F9-9A87-4B84-8CAD-0981B9366330"}
 * @AllowToRunInFind
 */
function onShow(firstShow, event) {
	// hace busqueda para que quede vacío el form
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)  // 16/09/2010
	controller.find()
	cuentas_cyb_id=0
	controller.search()
	fEgresos 	= 0
	fIngresos 	= 0
	fNumEgr 	= 0
	fNumIng 	= 0
	fNumMovim 	= 0
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"B6AEDE98-84C6-462A-94F6-5666ED9BAC98"}
 */
function onAction_anula_periodo(event) {
	globals.gBancoSaldo = null
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"7F1FE3F0-1010-405B-AEF4-3B5D5BFA0E62"}
 */
function onDataChange_banco_saldo(oldValue, newValue, event) {
	if (newValue != null)
	{
		actualizaDiferencia()
		fFechaDesde = gsaldos_banco_to_saldos_banco.fecha_inicial
		fFechaHasta = gsaldos_banco_to_saldos_banco.fecha_final
	}
	return true
}

/**
 * @properties={typeid:24,uuid:"DD82CCC4-47C3-4BF8-ABE3-340E2FDC4AC6"}
 */
function actualizaDiferencia()
{
	/*var query = 'SELECT SUM(importe_signo) as "total" '+
			'FROM movim_cyb where userv_id = ? and ' +
			'marca_conciliacion = 1 and cuentas_cyb_id = ? and saldos_banco_id = ? '+
			'group by cuentas_cyb_id, userv_id'*/ 
	//  revisar x que no funciona en Logistica lo de arriba
	var query = 'SELECT SUM(importe_signo) as "total" '+
			'FROM movim_cyb where ' +
			'marca_conciliacion = 1 and cuentas_cyb_id = ? and saldos_banco_id = ? '+
			'group by cuentas_cyb_id'
	var args = new Array
	args[0] = globals.gCuentaConc
	args[1] = globals.gBancoSaldo
	//args[2] = globals.gUservID // revisar x que no funciona en Logistica
	
	var $ds 	= databaseManager.getDataSetByQuery('infocom',query,args,-1)
	
	var total 	= $ds.getValue(1,1)
	fDifSaldos 	= gsaldos_banco_to_saldos_banco.saldo_final - gsaldos_banco_to_saldos_banco.saldo_inicial - total 
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"C6F26724-D725-43F6-92F2-4599C3916200"}
 */
function onAction_totales(event) {
	fNumMovim = databaseManager.getFoundSetCount(foundset)
	fEgresos 	= 0
	fIngresos 	= 0
	fNumEgr 	= 0
	fNumIng 	= 0
	for (var i = 1 ; i <= fNumMovim ; i++)
	{
		var $rcd = foundset.getRecord(i)
		
		if ($rcd.cod_movim == 1)
		{
			fIngresos += $rcd.importe
			fNumIng ++
		}
		else
		{
			fEgresos += $rcd.importe
			fNumEgr ++
		}
	}
}

/**
 * @properties={typeid:24,uuid:"BC2253DF-5E16-4498-B492-4010645F7059"}
 */
function pdf() {
	var $imp = forms.concialiacion_impresion
	
	$imp.fCuenta 			= movim_cyb_to_cuentas_cyb.descripcion
	$imp.fFechaDesde 		= utils.dateFormat(fFechaDesde,'dd-MM-yyyy')
	$imp.fFechaHasta 		= utils.dateFormat(fFechaHasta,'dd-MM-yyyy')
	$imp.fBanco_saldo_id 	= globals.gBancoSaldo
	$imp.fDifSaldos 		= fDifSaldos
	
	globals.metodoPdf('concialiacion_impresion', 'conciliacion', true)
}
